import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from datetime import datetime


class Base():
    def __init__(self, driver):
        self.driver = driver

    def open_url(self, url):
        self.driver.get(url)

    def getcwd(self):
        name = time.strftime("%Y%m%d%H%M%S")
        return name

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(EC.visibility_of_element_located(loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            self.driver.save_screenshot(self.getcwd() + '.png')
            print(f"异常打印{e}")

    def click(self, loc):
        self.find_element(loc).click()

    def clear(self, loc):
        self.find_element(loc).clear()

    def send_keys(self, loc, test):
        self.find_element(loc).send_keys(test)

    # def switch_to_frame(self, loc):
    #     frame_loc = self.find_element(loc)
    #     self.driver.switch_to.frame(frame_loc)

    def switch_to_frame(self, n):
        self.driver.switch_to.frame(n)

    def switch_file(self):
        self.driver.switch_to.default_content()
